<?php

App::import('Sanitize');

class WallsController extends AppController {
	
	var $helper = array('Html', 'Form');
	var $name = 'Walls';
	var $uses = array('User', 'Post');
	
	
	function index() {
			if ($this->Session->read('account_type') == 'user') {
				$user = $this->Session->read('user');
				$posts = $this->Post->find('all', array('conditions'=>array('Post.user_id'=>$user['User']['id'])));
			
				$data = array(
					'user' => $user,
					'posts' => $posts
				);
				$this->set($data);
			}
		
	}
	
	function update_status() {
		
		/* if a new status is submitted. */
		if (!empty($this->data)) {
			$user = $this->Session->read('user');
			$post_content = $this->data['Wall']['post_content'];
			
			$post = array();
			$post['Post']['user_id'] = $user['User']['id'];
			$post['Post']['poster_id'] = $user['User']['id'];
			$post['Post']['content'] = $post_content;
			$post['Post']['timestamp'] = DboSource::expression('NOW()');
			$this->Post->save($post);
		
			$this->redirect(array('controller'=>'walls', 'action'=>'index'));
		} 
	}
	
	function write_post($user_id = null) {
		if (!empty($this->data)) {
			$user = $this->Session->read('user');
			$post_content = $this->data['Wall']['post_content'];
			
			$post = array();
			$post['Post']['user_id'] = $user_id;
			$post['Post']['poster_id'] = $user['User']['id'];
			$post['Post']['content'] = $post_content;
			$post['Post']['timestamp'] = DboSource::expression('NOW()');
			$this->Post->save($post);
		
			$this->redirect(array('controller'=>'users', 'action' => 'profile/' . $user_id));
		} 
		
	}
}

?>